// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FinalScreen.cs" company="">
//   
// </copyright>
// <summary>
//   The final screen.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

/*
 * Author: Juan Mompean Esteban
 * Created: 6-12-2011
 */
namespace ITU_vs_Zombies.Menu.Screens
{
    using System;

    using ITU_vs_Zombies.Game;

    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Audio;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.Input;

    /// <summary>
    /// The final screen.
    /// </summary>
    public class FinalScreen : GameScreen
    {
        #region Constants and Fields

        /// <summary>
        /// The sound extra.
        /// </summary>
        [NonSerialized]
        public SoundEffect SoundExtra;

        /// <summary>
        /// The message.
        /// </summary>
        private readonly string message;

        /// <summary>
        /// The content.
        /// </summary>
        private ContentManager content;

        /// <summary>
        /// The is playing.
        /// </summary>
        private bool isPlaying;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="FinalScreen"/> class.
        /// </summary>
        /// <param name="message">
        /// The message.
        /// </param>
        /// <param name="soundExtra">
        /// The sound extra.
        /// </param>
        public FinalScreen(string message, string soundExtra)
        {
            this.message = message;
            this.SoundExtraName = soundExtra;
            this.isPlaying = false;
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets SoundExtraName.
        /// </summary>
        public string SoundExtraName { get; set; }

        #endregion

        #region Public Methods

        /// <summary>
        /// The activate.
        /// </summary>
        /// <param name="instancePreserved">
        /// The instance preserved.
        /// </param>
        public override void Activate(bool instancePreserved)
        {
            if (instancePreserved)
            {
                return;
            }

            if (this.content == null)
            {
                this.content = new ContentManager(this.ScreenManager.Game.Services, "Content");
            }

            this.SoundExtra = this.content.Load<SoundEffect>(this.SoundExtraName);
        }

        /// <summary>
        /// The draw.
        /// </summary>
        /// <param name="gameTime">
        /// The game time.
        /// </param>
        public override void Draw(GameTime gameTime)
        {
            if (!this.isPlaying)
            {
                this.SoundExtra.Play();
                this.isPlaying = true;
            }

            this.ScreenManager.SpriteBatch.Begin();
            var position =
                new Vector2(
                    (this.ScreenManager.GraphicsDevice.Viewport.Width / 2)
                    - (this.ScreenManager.Font.MeasureString(this.message).X / 2), 
                    this.ScreenManager.GraphicsDevice.Viewport.Height / 2);
            this.ScreenManager.SpriteBatch.DrawString(this.ScreenManager.Font, this.message, position, Color.Black);
            this.ScreenManager.SpriteBatch.End();
        }

        /// <summary>
        /// The handle input.
        /// </summary>
        /// <param name="gameTime">
        /// The game time.
        /// </param>
        /// <param name="input">
        /// The input.
        /// </param>
        public override void HandleInput(GameTime gameTime, InputState input)
        {
            var inputAction = new InputAction(new[] { Keys.Escape }, true);
            if (inputAction.Evaluate(input))
            {
                Client.GameState = GameStateEnum.Normal;
                this.ExitScreen();
            }
        }

        /// <summary>
        /// The unload.
        /// </summary>
        public override void Unload()
        {
            if (this.content != null)
            {
                this.content.Unload();
            }
        }

        #endregion
    }
}